package com.yyk.yykywd.aa;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.yyk.yykywd.R;
import com.yyk.yykywd.bean.IndexLinelistInfo;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.WINDOW_SERVICE;

/**
 * 底部横向表格图
 */

public class HorinalBarChartManager {
    private Context context;
    private HorizontalBarChart horizontalBarChart;

    public HorinalBarChartManager(Context context, HorizontalBarChart horizontalBarChart) {
        this.context = context;
        this.horizontalBarChart = horizontalBarChart;
        barSetting();
    }

    private List<IndexLinelistInfo.StudycountBean> dataBean;

    public void initData(List<IndexLinelistInfo.StudycountBean> dataBean) {
        if (dataBean != null) {
            this.dataBean = dataBean;
        }

        ArrayList<BarEntry> bookNumList = new ArrayList<>();
        ArrayList<BarEntry> pactNumList = new ArrayList<>();
        for (int i = 0; i < dataBean.size(); i++) {
            BarEntry barEntryBook = new BarEntry(i + 1, dataBean.get(i).getBooknum(), dataBean.get(i).getBooknum());
            bookNumList.add(barEntryBook);
            BarEntry barEntryPact = new BarEntry(i + 1, dataBean.get(i).getPactnum(), dataBean.get(i).getPactnum());
            pactNumList.add(barEntryPact);
        }
        //图形 柱高度
        int height = bookNumList.size() * dp2px(context, 60);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);
        horizontalBarChart.setLayoutParams(layoutParams);

        BarDataSet barDataSetBook = new BarDataSet(bookNumList, "项目申请书数量");
        barDataSetBook.setColor(context.getResources().getColor(R.color.blue_64));
        BarDataSet barDataSetPact = new BarDataSet(pactNumList, "签订合同数量");
        barDataSetPact.setColor(context.getResources().getColor(R.color.green_74));
        ArrayList<IBarDataSet> barDataSets = new ArrayList<>();
        barDataSets.add(barDataSetBook);
        barDataSets.add(barDataSetPact);
        BarData barData = new BarData(barDataSets);
        int barAmount = 2; //需要显示柱状图的类别 数量
        float groupSpace = 0.2f; //柱状图组之间的间距
        float barWidth = (1f - groupSpace) / barAmount;
        float barSpace = 0f;
        barData.setBarWidth(barWidth);
        barData.groupBars(0.5f, groupSpace, barSpace);
        horizontalBarChart.animateX(2000);
        horizontalBarChart.animateY(2000);
        horizontalBarChart.setData(barData);

        horizontalBarChart.invalidate();
    }

    private void barSetting() {

        horizontalBarChart.setDrawBorders(false);//边框是否显示
        horizontalBarChart.setDrawGridBackground(false);//网格是否显示
        horizontalBarChart.setTouchEnabled(false);
//        horizontalBarChart.animateX(2000);
//        horizontalBarChart.animateY(2000);
        //右下角描述
        Description description = new Description();
        description.setEnabled(false);
        horizontalBarChart.setDescription(description);
        //保证Y轴从0开始
        YAxis axisLeft = horizontalBarChart.getAxisLeft();
        YAxis axisRight = horizontalBarChart.getAxisRight();
        axisLeft.setAxisMinimum(0.1f);
        axisLeft.setDrawAxisLine(true);
        axisLeft.setDrawGridLines(true);
        axisLeft.setEnabled(false);
        axisLeft.setGranularity(2);
        axisRight.setAxisMinimum(0.1f);
        axisRight.setDrawAxisLine(true);
        axisRight.setDrawGridLines(true);
        axisRight.setGranularity(2);
        //X轴设置
        XAxis xAxis = horizontalBarChart.getXAxis();
        xAxis.setAxisMinimum(0.5f);
        xAxis.setGranularity(1);
        xAxis.setLabelCount(12, false);
        xAxis.setDrawAxisLine(true);//不显示x轴线条
        xAxis.setDrawGridLines(false);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
//                return dataBean.get(((int) value) - 1).getMonth();
                return String.valueOf(dataBean.get((int) value-1).getMonth());
            }
        });
        // 设置表格标示的位置
        Legend l = horizontalBarChart.getLegend();
        l.setEnabled(false);
        /**
         * 去掉圆点描述
         */
//        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
//        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
//        l.setOrientation(Legend.LegendOrientation.VERTICAL);
//        l.setDrawInside(false);
//        l.setForm(Legend.LegendForm.DEFAULT);
//        l.setFormSize(9f);
//        l.setTextSize(11f);

    }

    private int dp2px(Context context, int dpValue) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager systemService = (WindowManager) context.getSystemService(WINDOW_SERVICE);
        systemService.getDefaultDisplay().getMetrics(displayMetrics);
        return (int) (dpValue * displayMetrics.density + 0.5f);
    }
}
